package com.example.cofeed.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.cofeed.R;
import com.example.cofeed.activity.SelectCityActivity;
import com.example.cofeed.adapter.SubscriptionAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseFragment;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.entity.Subscription;
import com.lzy.okgo.model.Response;

import java.util.List;

import butterknife.BindView;

/**免费
 * Created by mqwang on 2017/9/18.
 */

public class TabFreeSubscriptionFragment extends BaseFragment {
    @BindView(R.id.rec_lv)
    RecyclerView recLv;
    private SubscriptionAdapter subscriptionAdapter;

    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_free_subscription, container, false);
        return view;
    }

    @Override
    protected void initData() {

        subscriptionAdapter = new SubscriptionAdapter(0);
        recLv.setLayoutManager(new LinearLayoutManager(getActivity()));
        recLv.setAdapter(subscriptionAdapter);
        subscriptionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), SelectCityActivity.class);
                intent.putExtra(IntentKeyConstant.CATALOG_ID, ((Subscription) adapter.getData().get(position)).catalogid);
                intent.putExtra(IntentKeyConstant.CATALOG_NAME, ((Subscription) adapter.getData().get(position)).title);
                intent.putExtra(IntentKeyConstant.TYPE, 2);
                startActivity(intent);

            }
        });

        getData();

//        Subscription subscription = new Subscription();
//        subscription.citys = "北京、上海、深圳";
//        subscription.title = "大豆";
//
//        subscriptionAdapter.addData(subscription);

    }

    private void getData() {
        ApiService.subscribe(this, 2, new DialogCallback<LzyResponse<List<Subscription>>>(getActivity()) {
            @Override
            public void onSuccess(Response<LzyResponse<List<Subscription>>> response) {
                subscriptionAdapter.setNewData(response.body().data);

            }
        });
    }

}
